﻿using Direct.IDAL.Manager.Money;
using DirectSell.Common.Url;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.UserCenter
{
    /// <summary>
    /// 提现审核仓储类
    /// </summary>
    public class RealMoneyAuditRepository:BaseManagerRepository, IRealMoneyAuditRepository
    {

        #region 查询提现记录
        /// <summary>
        /// 查询提现记录
        /// </summary>
        /// <returns></returns>
        public async Task<PagedList<RealMoney>> QueryRecord(BaseCondition condition)
        {
            PageParam model = new PageParam();
            model.OrderBy = "State ASC";
            model.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            model.PageSize = condition.pageSize;
            model.TableName = "RealMoneyApply";
            

            StringBuilder sb = new StringBuilder();
            sb.Append(" 1=1");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat("AND (UserName LIKE '%{0}%' OR UserCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }
            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ApplyTime>='{0}' and ApplyTime<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND ApplyTime>='{0}' and ApplyTime<='{1}' ", condition.startTime, DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ApplyTime>='2010-01-01' and ApplyTime<='{1}' ", condition.endTime);
                }
            }
            model.StrWhere = sb.ToString();
            return await base.PageQuery<RealMoney>(model);
        }
        #endregion

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        public void Audit(int id,int type,ref Hashtable hstable)
        {
            string sql = string.Format(" UPDATE RealMoneyApply SET State={0},HandleTime=GETDATE() WHERE Id = {1} ", type,id);
            hstable.Add(sql, null);
        }
        #endregion

        #region 审核不通过退款
        /// <summary>
        /// 审核不通过退款
        /// </summary>
        /// <param name="usercode"></param>
        public void Refund(string usercode,decimal money,ref Hashtable hstable)
        {
            string sql =string.Format(" UPDATE UserInforMations SET RealMoney+={0} WHERE UserCode='{1}' ",money,usercode);
            hstable.Add(sql, null);
        }
        #endregion

    }
}
